<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording"></searchuinew>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <div class="panel-line flex-end">
          <el-button size="mini" type="primary" @click="handleAdd">新增</el-button>
          <!-- <el-button size="mini" type="error" @click="handleDelete">删除</el-button> -->
        </div>
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50"> </el-table-column>
          <el-table-column align="center" header-align="center" label="规则名称" prop="name" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="规则描述" prop="desc" width="300"></el-table-column>
          <el-table-column align="center" header-align="center" label="适用范围" prop="range_name" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="跟踪倒计时" prop="is_accept_down_text" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="申领地区" prop="zone_province" width="250"></el-table-column>
          <el-table-column align="center" header-align="center" label="申领上线(个)" prop="accept_limit" width="200"></el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleEdit(scope)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
      <!-- 弹窗-->
      <popup @refreshList="getDataList" ref="popup"></popup>
    </div>
  </div>
</template>

<script>
import { apiRuleList, apiRuleAdd, apiRuleEdit, apiRuleDel, apiRuleSelect } from '@/api/customer'
import popup from './popup'
export default {
  name: 'index',
  components: { popup },
  data() {
    return {
      searchFormArr: [
        {
          fieldName: '规则名称：',
          type: 'input',
          field: 'name',
          placeholder: '请输入规则名称',
        },
        {
          fieldName: '规则描述：',
          type: 'input',
          field: 'desc',
          placeholder: '请输入规则描述',
        },
        {
          fieldName: '适用范围：',
          type: 'input',
          field: 'dept_name',
          placeholder: '请输入适用范围',
        },
        {
          fieldName: '申请地区：',
          type: 'select',
          field: 'zone',
          option: [],
          placeholder: '请选择申请地区',
          multiple: true,
        },
      ],
      isAccording: true,
      searchParams: {
        name: '',
        desc: '',
        dept_name: '',
        zone: '',
      },

      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      multipleSelection: [],
    }
  },
  mounted() {
    this.getDataList()
    this.getSelect()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize, sts: this.tabActive }, this.searchParams)
      apiRuleList(info)
        .then((res) => {
          const { items, total } = res.result
          items.forEach((item) => {
            item.is_accept_down_text = parseInt(item.is_accept_down) === 1 ? '开启' : '关闭'
          })
          this.dataList = items
          this.totalPage = total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 获取下拉信息
    getSelect() {
      apiRuleSelect().then((res) => {
        const { zone } = res.data
        this.searchFormArr.find((item) => {
          return item.field === 'zone'
        }).option = zone.map((item) => {
          return {
            id: item,
            text: item,
          }
        })
      })
    },
    // 列表查询
    searchParma(e) {
      this.searchFormArr.forEach((item) => {
        if (item.field === 'time') return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.zone && e.zone.length > 0) {
        this.searchParams.zone = e.zone.join(',')
      } else {
        this.searchParams.zone = ''
      }
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 修改
    handleEdit(scope) {
      this.$refs.popup.open(scope.row)
    },
    // 新增
    handleAdd() {
      this.$refs.popup.open()
    },
    // 删除
    handleDelete() {
      if (this.multipleSelection.length <= 0) {
        this.$message({
          type: 'error',
          message: '请至少选择一项数据!',
        })
        return
      }
      this.$confirm('此操作将删除该规则, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.deleteConfirm()
      })
    },
    // 删除确认
    deleteConfirm() {
      this.detailLoading = true
      apiRuleDel({ id: this.multipleSelection.join(',') })
        .then((res) => {
          this.detailLoading = false
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!',
            })
            this.multipleSelection.forEach((selectItem) => {
              const index = this.dataList.findIndex((item) => {
                return item.id === selectItem
              })
              if (index !== -1) {
                this.dataList.splice(index, 1)
              }
            })
          }
        })
        .catch(() => {
          this.detailLoading = false
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => {
        return item.id
      })
    },
  },
}
</script>

<style scoped lang="scss">
.panel-line {
  margin-bottom: 16px;
}
.grey {
  color: #999999;
}
</style>
